import { shopList } from "network/shop"

export default {
  namespaced: true,
  state: () => ({
    cart: []
  }),
  mutations: {
    updateCart(state, cart) {
      state.cart = cart
    },
    updateState(state, {id, value}) {
      state.cart[state.cart.findIndex(item => item.id === id)].goods_state = value
    },
    updateCount(state, {id, value}) {
      state.cart[state.cart.findIndex(item => item.id === id)].goods_count = value
    },
    updateAllState(state, val) {
      state.cart.filter(item => item.goods_state = val)
    }
  },
  actions: {
    async getCart({ commit }) {
      let res = await shopList()
      commit('updateCart', res.data.list)
    }
  },
  getters: {
    getPriceAll(state) {
      return state.cart.filter(item => item.goods_state).reduce((old, item) => old + item.goods_price * item.goods_count, 0)
    },
    getCountAll(state) {
      return state.cart.filter(item => item.goods_state).reduce((old, item) => old + item.goods_count, 0)
    }
  }
}